package com.cc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.dto.DishDTO;
import com.cc.model.Dish;
import com.cc.service.DishService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dishes")
@Tag(name = "菜品管理", description = "管理菜品信息的相关操作")
@CrossOrigin
public class DishController {

    @Autowired
    private DishService dishService;

    @Operation(summary = "获取所有菜品", description = "分页获取菜品列表")
    @GetMapping
    public ResponseEntity<Page<Dish>> getAllDishes(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size,
            @RequestParam(required = false) String name
    ) {
        Page<Dish> dishPage = dishService.list(page, size, name);
        return ResponseEntity.ok(dishPage);
    }

    @Operation(summary = "根据分类名称模糊查询菜品", description = "根据分类名称分页获取菜品列表")
    @GetMapping("/category/name")
    public ResponseEntity<Page<Dish>> getDishesByCategoryName(
            @RequestParam(required = false) String categoryId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size
    ) {
        Page<Dish> dishPage = dishService.listByCategoryName(categoryId, page, size);
        return ResponseEntity.ok(dishPage);
    }

    @Operation(summary = "创建菜品", description = "添加新的菜品信息")
    @PostMapping
    public ResponseEntity<Dish> createDish(@RequestBody DishDTO dishDTO) {
        Dish createdDish = dishService.create(dishDTO);
        return ResponseEntity.ok(createdDish);
    }

    @Operation(summary = "根据名称获取菜品", description = "根据菜品名称获取菜品信息")
    @GetMapping("/name/{name}")
    public ResponseEntity<Dish> getDishByName(@PathVariable String name) {
        Dish dish = dishService.getByName(name);
        return ResponseEntity.ok(dish);
    }

    @Operation(summary = "更新菜品", description = "根据ID更新菜品信息")
    @PutMapping("/{id}")
    public ResponseEntity<Dish> updateDish(@PathVariable Long id, @RequestBody DishDTO dishDTO) {
        Dish updatedDish = dishService.update(id, dishDTO);
        return ResponseEntity.ok(updatedDish);
    }

    @Operation(summary = "删除菜品", description = "根据ID删除指定的菜品")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDish(@PathVariable Long id) {
        dishService.delete(id);
        return ResponseEntity.noContent().build();
    }
} 